package com.hightemperator.controller;

import com.hightemperator.po.*;
import com.hightemperator.service.BackGroundUserService;
import com.hightemperator.service.HomePageService;
import com.hightemperator.service.UserService;
import com.hightemperator.util.Constant;
import com.hightemperator.util.LocalResponseBody;
import com.hightemperator.util.UploadUtil;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/background/backgroundController")
public class BackGroundUserController {

    @Autowired
    private BackGroundUserService backGroundUserService;
    @Autowired
    private UserService userService;
    @Autowired
    private HomePageService homePageService;

    /**
     * 后台管理系统 登陆
     *
     * @param sysBackgroundUser
     * @return
     */
    @RequestMapping("login")
    public LocalResponseBody login(SysBackgroundUser sysBackgroundUser,
                                   HttpServletResponse response) {

        List<SysBackgroundUser> sysBackgroundUserList = backGroundUserService.getBackgroundUser(sysBackgroundUser);
        if (sysBackgroundUserList.size() != 0) {
            return new LocalResponseBody("登陆成功").doSuccess();
        } else {
            return new LocalResponseBody("账号或密码错误").doFail();
        }

    }

    @RequestMapping("getUserList")
    public LocalResponseBody getUserList(HttpServletResponse response,
                                         HttpServletRequest request) {

        String page = request.getParameter("page");
        String limit = request.getParameter("limit");
        Integer begin;
        Integer end;
        if ((page!=null&&!page.equals("")||(limit!=null&&!limit.equals("")))){
             begin = (Integer.parseInt(page) - 1) * Integer.parseInt(limit);
             end = Integer.parseInt(limit);
        }else{
            begin = 0;
            end = Integer.MAX_VALUE;
        }

        Map<String,Object> map = backGroundUserService.getUserList(begin, end);
        List<SysUser> sysUserList = (List<SysUser>) map.get("list");

        Integer count = (Integer) map.get("count");

        return new LocalResponseBody("操作成功",count.toString()).doSuccess(sysUserList);
    }


    /**
     * 通过id查询用户
     *
     * @param uId  用户id
     * @return
     */
    @RequestMapping("getUserById")
    public LocalResponseBody getUserById(HttpServletResponse response
            ,@RequestParam(value = "uId", required = false, defaultValue = "") Integer uId) {

        SysUser sysUser = userService.getUserMessage(uId);
        if (sysUser!=null){
            return new LocalResponseBody("操作成功").doSuccess(sysUser);
        }else{
            return new LocalResponseBody("操作失败").doFail();
        }
    }

    /**
     * 获取 轮播图列表
     * @return
     */
    @RequestMapping("getBannerList")
    public LocalResponseBody getBannerList(HttpServletRequest request){
        String page = request.getParameter("page");
        String limit = request.getParameter("limit");
        Integer begin;
        Integer end;
        if ((page!=null&&!page.equals("")||(limit!=null&&!limit.equals("")))){
            begin = (Integer.parseInt(page) - 1) * Integer.parseInt(limit);
            end = Integer.parseInt(limit);
        }else{
            begin = 0;
            end = Integer.MAX_VALUE;
        }
        Map<String,Object> map = backGroundUserService.getBannerList(begin, end);
        List<Banner> bannerList = (List<Banner>) map.get("list");

        Integer count = (Integer) map.get("count");

        return new LocalResponseBody("操作成功",count.toString()).doSuccess(bannerList);
    }

    /**
     * 添加轮播图
     * @return
     */
    @RequestMapping("addBanner")
    public LocalResponseBody addBanner(HttpServletRequest request, Banner banner){
        boolean isMultipart = ServletFileUpload.isMultipartContent(request);
        int successCode = 0;
        if (isMultipart) {
            MultipartFile files = ((MultipartHttpServletRequest) request).getFile("files");
            if (files != null) {
                String tomcatPath = System.getProperty("catalina.home");
                String path = UploadUtil.uploadFileToPath(new MultipartFile[]{files}, tomcatPath + Constant.SPEADER + "picture", "banner");
                banner.setBannerpath(path);
                successCode = backGroundUserService.addBanner(banner);
            }
        }
        if (successCode == 0){
            return new LocalResponseBody("操作失败").doFail();
        }else{
            return new LocalResponseBody("操作成功").doSuccess();
        }
    }

}
